﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataManager;
using DataManager.BLL;

namespace FILMEK.GUI
{
    public partial class frmVasarlokalMuveletek : Form
    {
        FilmManager manager;
        string oldName, oldAddress, oldLocation, oldEmail;

        public frmVasarlokalMuveletek(FilmManager manager)
        {
            InitializeComponent();
            this.manager = manager;
            fillDgvVasarlok();
        }

        public void fillDgvVasarlok()
        {
            DataSet d = manager.vasarlokAdatai();
            dgvVasarlok.DataSource = d.Tables[0];
        }

        private void btTorol_Click(object sender, EventArgs e)
        {
            int vasarloId = Convert.ToInt32(dgvVasarlok.SelectedRows[0].Cells[0].Value);
            DialogResult dlgResult = MessageBox.Show("Biztos törlni akarod?", "Törlünk, törlünk? [-(", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlgResult == DialogResult.Yes)
            {
                manager.vasarloTorlese(vasarloId);
            }
            fillDgvVasarlok();
        }

        private void cbModosit_CheckedChanged(object sender, EventArgs e)
        {
            if (cbModosit.Checked)
            {
                tbNev.Text = dgvVasarlok.SelectedRows[0].Cells[1].Value.ToString();
                tbCim.Text = dgvVasarlok.SelectedRows[0].Cells[2].Value.ToString();
                tbHelyseg.Text = dgvVasarlok.SelectedRows[0].Cells[3].Value.ToString();
                tbEmail.Text = dgvVasarlok.SelectedRows[0].Cells[4].Value.ToString();
            }
            else
            {
                clearTextBoxes();
            }
        }

        private void clearTextBoxes()
        {
            tbNev.Text = "";
            tbCim.Text = "";
            tbHelyseg.Text = "";
            tbEmail.Text = "";
        }

        private void dgvVasarlok_selectionChanged(object sender, EventArgs e)
        {
            int id;
            try
            {
                id = Convert.ToInt32(dgvVasarlok.SelectedRows[0].Cells[0].Value);
            }
            catch
            {
                id = 0;
            }
            if (id > 0)
            {
                DataSet ds = manager.getVasarlokAdatai(id);
                oldName = ds.Tables[0].Rows[0][0].ToString();
                oldAddress = ds.Tables[0].Rows[0][1].ToString();
                oldLocation = ds.Tables[0].Rows[0][2].ToString();
                oldEmail = ds.Tables[0].Rows[0][3].ToString();
                //MessageBox.Show("Megvan!");
            }
        }

        private void btElment_Click(object sender, EventArgs e)
        {
            if (cbModosit.Checked)
            {
                int id = Convert.ToInt32(dgvVasarlok.SelectedRows[0].Cells[0].Value);
                manager.vasarlotFrissitOptimista(id, tbNev.Text, tbHelyseg.Text, tbCim.Text, tbEmail.Text, oldName, oldLocation, oldAddress, oldEmail);
                DataSet d = manager.getVasarlokAdatai(id);

                if ((d.Tables[0].Rows[0][0].ToString().CompareTo(tbNev.Text) == 0)
                    && (d.Tables[0].Rows[0][1].ToString().CompareTo(tbCim.Text) == 0)
                    && (d.Tables[0].Rows[0][2].ToString().CompareTo(tbHelyseg.Text) == 0)
                    && (d.Tables[0].Rows[0][3].ToString().CompareTo(tbEmail.Text) == 0))
                {
                    MessageBox.Show("A valtoztatasok el lettek mentve!");
                }
                else
                {
                    switch (MessageBox.Show("Ido kozben modosult mar az adatbazis elem. Kivanja megis menteni a valtoztatasokat?",
                           "WonderWord",
                           MessageBoxButtons.YesNoCancel,
                           MessageBoxIcon.Question))
                    {
                        case DialogResult.Yes:
                            // "Yes" vasarlokat frissit
                            manager.vasarlotFrissit(id, tbNev.Text, tbHelyseg.Text, tbCim.Text, tbEmail.Text);
                            break;

                        case DialogResult.No:
                            // "No" visszallitja a regit
                            manager.vasarlotFrissit(id, oldName, oldLocation, oldAddress, oldEmail);
                            break;

                        case DialogResult.Cancel:
                            // "Cancel" hagyja a mostanit
                            break;
                    }
                }
            }
            else
            {
                int id = Convert.ToInt32(manager.getMaxVasarlokID().Tables[0].Rows[0][0]) + 1;
                manager.vasarlotBeszur(id, tbNev.Text, tbHelyseg.Text, tbCim.Text, tbEmail.Text);
            }

            fillDgvVasarlok();
            clearTextBoxes();
        }

    }
}
